package com.kazimsoylu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import javax.swing.Timer;

import net.sf.jml.MsnContact;
import net.sf.jml.MsnContactList;
import net.sf.jml.MsnContactPending;
import net.sf.jml.MsnGroup;
import net.sf.jml.MsnList;
import net.sf.jml.MsnMessenger;
import net.sf.jml.MsnSwitchboard;
import net.sf.jml.MsnUserStatus;
import net.sf.jml.event.MsnContactListAdapter;
import net.sf.jml.event.MsnSwitchboardAdapter;
import net.sf.jml.message.MsnControlMessage;
import net.sf.jml.message.MsnInstantMessage;

import com.kazimsoylu.gui.Messenger;

public class ContactListHandler extends MsnContactListAdapter
{

	@Override
	public void contactAddCompleted(MsnMessenger arg0, MsnContact arg1,
			MsnList arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contactAddInGroupCompleted(MsnMessenger arg0, MsnContact arg1,
			MsnGroup arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contactAddedMe(MsnMessenger arg0, MsnContact arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contactAddedMe(MsnMessenger arg0, MsnContactPending[] arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contactListInitCompleted(MsnMessenger messenger) 
	{
		final MsnContact[] contacts = messenger.getContactList().getContactsInList(MsnList.AL);		
		final Object id = new Object();

		messenger.addSwitchboardListener(new MsnSwitchboardAdapter() {

            public void switchboardStarted(MsnSwitchboard switchboard) 
            {
                if (id != switchboard.getAttachment())
                    return;
                for (MsnContact contact : contacts) 
                {
                    //don't send message to offline contact
                    if (contact.getStatus() != MsnUserStatus.OFFLINE) 
                    {
                        switchboard.inviteContact(contact.getEmail());
                        Application.addSwitchBoard( contact , switchboard);
                    }
                }
            }

            public void switchboardClosed(MsnSwitchboard switchboard) {
                switchboard.getMessenger().removeSwitchboardListener(this);
            }

        });
        messenger.newSwitchboard(id);
        
        

	}

	@Override
	public void contactListSyncCompleted(final MsnMessenger messenger) 
	{
		Messenger.getInstance().fillContactList( messenger.getContactList().getContacts() );
			
	}

	@Override
	public void contactPersonalMessageChanged(MsnMessenger arg0, MsnContact arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contactRemoveCompleted(MsnMessenger arg0, MsnContact arg1,
			MsnList arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contactRemoveFromGroupCompleted(MsnMessenger arg0,
			MsnContact arg1, MsnGroup arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contactRemovedMe(MsnMessenger arg0, MsnContact arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contactStatusChanged(MsnMessenger arg0, MsnContact arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void groupAddCompleted(MsnMessenger arg0, MsnGroup arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void groupRemoveCompleted(MsnMessenger arg0, MsnGroup arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ownerDisplayNameChanged(MsnMessenger arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ownerStatusChanged(MsnMessenger arg0) {
		// TODO Auto-generated method stub
		
	}

}
